
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.postorder.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxk.oms.postorder.domain.OmsPostOrderItem;
import com.lxk.oms.postorder.domain.bo.OmsPostOrderItemBo;
import com.lxk.oms.postorder.domain.vo.OmsPostOrderItemVo;
import com.lxk.oms.postorder.mapper.OmsPostOrderItemMapper;
import com.lxk.oms.postorder.service.IOmsPostOrderItemService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单售后明细Service业务层处理
 *
 * @author LXK-RY5
 * @date 2024-09-28
 */
@RequiredArgsConstructor
@Service
public class OmsPostOrderItemServiceImpl implements IOmsPostOrderItemService {

    private final OmsPostOrderItemMapper baseMapper;


    /**
     * 查询符合条件的订单售后明细列表
     *
     * @return 订单售后明细列表
     */
    @Override
    public List<OmsPostOrderItemVo> queryPostOrderId(Long postOrderId) {
        OmsPostOrderItemBo bo = new OmsPostOrderItemBo();
        bo.setPostOrderId(postOrderId);
        LambdaQueryWrapper<OmsPostOrderItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

   /**
    * 查询IdList订单售后明细Map
    */
    @Override
    public Map<Long, OmsPostOrderItemVo> queryMapByIdList(List<Long> idList) {
        return this.queryByIdList(idList).stream().collect(Collectors.toMap(OmsPostOrderItemVo::getId, v -> v));
    }

    /**
     * 查询订单售后明细Map
     */
    @Override
    public Map<Long, OmsPostOrderItemVo> queryMap(OmsPostOrderItemBo bo) {
        return  this.queryList(bo).stream().collect(Collectors.toMap(OmsPostOrderItemVo::getId, v -> v));
    }

    /**
     * 查询订单售后明细IdList
     */
    @Override
    public List<OmsPostOrderItemVo> queryByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<OmsPostOrderItem> lqw = Wrappers.lambdaQuery();
        lqw.in(OmsPostOrderItem::getId, idList);
       return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询订单售后明细
     *
     * @param id 主键
     * @return 订单售后明细
     */
    @Override
    public OmsPostOrderItemVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询订单售后明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单售后明细分页列表
     */
    @Override
    public TableDataInfo<OmsPostOrderItemVo> queryPageList(OmsPostOrderItemBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OmsPostOrderItem> lqw = buildQueryWrapper(bo);
        Page<OmsPostOrderItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单售后明细列表
     *
     * @param bo 查询条件
     * @return 订单售后明细列表
     */
    @Override
    public List<OmsPostOrderItemVo> queryList(OmsPostOrderItemBo bo) {
        LambdaQueryWrapper<OmsPostOrderItem> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OmsPostOrderItem> buildQueryWrapper(OmsPostOrderItemBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OmsPostOrderItem> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(OmsPostOrderItem::getId);
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            OmsPostOrderItem::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            OmsPostOrderItem::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.eq(bo.getMerId() != null, OmsPostOrderItem::getMerId, bo.getMerId());
        lqw.eq(bo.getPostOrderId() != null, OmsPostOrderItem::getPostOrderId, bo.getPostOrderId());
        lqw.eq(bo.getOrderId() != null, OmsPostOrderItem::getOrderId, bo.getOrderId());
        lqw.eq(bo.getSpuId() != null, OmsPostOrderItem::getSpuId, bo.getSpuId());
        lqw.like(StringUtils.isNotBlank(bo.getSpuName()), OmsPostOrderItem::getSpuName, bo.getSpuName());
        lqw.eq(StringUtils.isNotBlank(bo.getSpuImage()), OmsPostOrderItem::getSpuImage, bo.getSpuImage());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuImage()), OmsPostOrderItem::getSkuImage, bo.getSkuImage());
        lqw.eq(bo.getSkuId() != null, OmsPostOrderItem::getSkuId, bo.getSkuId());
        lqw.like(StringUtils.isNotBlank(bo.getSkuName()), OmsPostOrderItem::getSkuName, bo.getSkuName());
        lqw.eq(bo.getUserId() != null, OmsPostOrderItem::getUserId, bo.getUserId());
        lqw.eq(bo.getRefundAmount() != null, OmsPostOrderItem::getRefundAmount, bo.getRefundAmount());
        lqw.eq(bo.getRefundCount() != null, OmsPostOrderItem::getRefundCount, bo.getRefundCount());
        return lqw;
    }

    /**
     * 新增订单售后明细
     *
     * @param bo 订单售后明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(OmsPostOrderItemBo bo) {
        OmsPostOrderItem add = MapstructUtils.convert(bo, OmsPostOrderItem.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单售后明细
     *
     * @param bo 订单售后明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(OmsPostOrderItemBo bo) {
        OmsPostOrderItem update = MapstructUtils.convert(bo, OmsPostOrderItem.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OmsPostOrderItem entity){
    }

    /**
     * 校验并批量删除订单售后明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
